
import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;

public class CalculDeux extends UnicastRemoteObject implements CalculInterface {
	final static char eps = ">".charAt(0);

	public static int min(int a, int b, int c) {
		return Math.min(Math.min(a, b), c);
	}

	final static int COLONNE = 0;
	final static int LIGNE = 1;

	private int orientation;
	private String ligne, colonne;

	public CalculDeux(int k, String a, String b) throws RemoteException {
		this.orientation = k;
		this.colonne = a;
		this.ligne = b;
	}

	public int[] calcul(int index, int precedent, int[] tab)
			throws RemoteException {
		return calculPourInt(index, precedent, tab);
	}

	public int[] calculPourInt(int index, int precedent, int[] tab) {
		int p = ligne.length();
		int n = colonne.length();
		int gauche;
		int dessous;
		int diag;
		int[] output;
		if (orientation == COLONNE) {
			output = new int[n - index+1];
		} else {
			output = new int[p - index+1];
		}
		int prec = precedent;

		output[0] = prec;
		
		for (int i = 1; i < output.length; i++) {
			if (orientation == LIGNE) {
				gauche = output[i-1];
				diag = tab[index+i-2];
				dessous = tab[index+i-1];
			} else {
				gauche = tab[index+i-1];
				diag = tab[index+i-2];
				dessous = output[i-1];
			}
		
			output[i] = calculValeur(gauche, dessous, diag, index, i);
		}

		return output;
	}

	public int calculValeur(int gauche, int dessous, int diag, int index, int l) {
		int i = 0;
		int j = 0;
		if (orientation == LIGNE) {
			i = index;
			j = index + l-1;
		} else {
			j = index;
			i = index + l-1;
		}
		
		int result = min(gauche + cost(ligne.charAt(i -1), eps), dessous
				+ cost(colonne.charAt(j - 1), eps),
				diag + cost(colonne.charAt(i), ligne.charAt(j)));
		return result;
	}

	public static int cost(char a, char b) {
		if (a == b)
			return 0;
		return 1;
	}

	public String getStringColonne() {
		return colonne;
	}

	public String getStringLigne() {
		
		return ligne;
	}
}
